package com.cherrypt.api.core.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cherrypt.api.core.dao.PostDao;
import com.cherrypt.api.core.service.PostService;
import com.cherrypt.api.model.dataobject.Post;
import javafx.geometry.Pos;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;


/**
 * @author intent
 * @date 2019/8/2 15:55
 * @about <link href='http://zzyitj.xyz/'/>
 */
@Service
public class PostServiceImpl extends ServiceImpl<PostDao, Post> implements PostService {
    private static final Logger logger = LoggerFactory.getLogger(PostServiceImpl.class);

    private IPage<Post> getPostPageDesc(int pageIndex, int pageSize, boolean desc, String field, boolean wait) {
        Page<Post> postPage = new Page<>(pageIndex, pageSize);
        if (desc) {
            String[] pinFiled = new String[3];
            pinFiled[0] = "pin";
            pinFiled[1] = "hot";
            pinFiled[2] = field;
            postPage.setDesc(pinFiled);
        } else {
            return this.baseMapper.selectPostPageOrderByAsc(postPage, field, wait);
        }
        return this.baseMapper.selectPostPage(postPage, wait);
    }

    @Override
    public IPage<Post> getPostPage(int pageIndex, int pageSize, boolean wait) {
        return getPostPageDesc(pageIndex, pageSize, true, "modify", wait);
    }

    @Override
    public IPage<Post> getPostPageOrderByCreated(int pageIndex, int pageSize, boolean desc, boolean wait) {
        return getPostPageDesc(pageIndex, pageSize, desc, "created", wait);
    }

    @Override
    public IPage<Post> getPostPageOrderByUid(int pageIndex, int pageSize, boolean desc, boolean wait) {
        return getPostPageDesc(pageIndex, pageSize, desc, "uid", wait);
    }

    @Override
    public IPage<Post> getPostPageOrderBySize(int pageIndex, int pageSize, boolean desc, boolean wait) {
        Page<Post> postPage = new Page<>(pageIndex, pageSize);
        if (desc) {
            return baseMapper.selectPostPageOrderBySizeDesc(postPage, wait);
        }
        return baseMapper.selectPostPageOrderBySizeAsc(postPage, wait);
    }

    @Override
    public IPage<Post> getPostPageOrderByTitle(int pageIndex, int pageSize, boolean desc, boolean wait) {
        return getPostPageDesc(pageIndex, pageSize, desc, "title", wait);
    }

    @Override
    public IPage<Post> getPostPageByCategoryName(int pageIndex, int pageSize, String categoryName, boolean wait) {
        return getPostPageByCategoryNameOrder(pageIndex, pageSize, categoryName, true, "modify", wait);
    }

    private IPage<Post> getPostPageByCategoryNameOrder(int pageIndex, int pageSize, String categoryName, boolean desc, String field, boolean wait) {
        Page<Post> postPage = new Page<>(pageIndex, pageSize);
        if (desc) {
            String[] pinFiled = new String[3];
            pinFiled[0] = "pin";
            pinFiled[1] = "hot";
            pinFiled[2] = field;
            postPage.setDesc(pinFiled);
        } else {
            return this.baseMapper.selectPostPageByCategoryNameOrderByAsc(postPage, categoryName, field, wait);
        }
        return this.baseMapper.selectPostPageByCategoryName(postPage, categoryName, wait);
    }

    @Override
    public IPage<Post> getPostPageByCategoryNameOrderByCreated(int pageIndex, int pageSize, String categoryName, boolean desc, boolean wait) {
        return getPostPageByCategoryNameOrder(pageIndex, pageSize, categoryName, desc, "created", wait);
    }

    @Override
    public IPage<Post> getPostPageByCategoryNameOrderByUid(int pageIndex, int pageSize, String categoryName, boolean desc, boolean wait) {
        return getPostPageByCategoryNameOrder(pageIndex, pageSize, categoryName, desc, "uid", wait);
    }

    @Override
    public IPage<Post> getPostPageByCategoryNameOrderByTitle(int pageIndex, int pageSize, String categoryName, boolean desc, boolean wait) {
        return getPostPageByCategoryNameOrder(pageIndex, pageSize, categoryName, desc, "title", wait);
    }

    @Override
    public IPage<Post> getPostPageByCategoryNameOrderBySize(int pageIndex, int pageSize, String categoryName, boolean desc, boolean wait) {
        Page<Post> postPage = new Page<>(pageIndex, pageSize);
        if (desc) {
            return baseMapper.selectPostPageByCategoryNameOrderBySizeDesc(postPage, categoryName, wait);
        }
        return baseMapper.selectPostPageByCategoryNameOrderBySizeAsc(postPage, categoryName, wait);
    }

    @Override
    public int getCount(boolean wait) {
        return this.count(new QueryWrapper<Post>().eq("is_wait", wait));
    }

    @Override
    public int getCountByCategoryName(String categoryName, boolean wait) {
        return this.baseMapper.selectCountByCategoryName(categoryName, wait);
    }

    @Override
    public int getCountByUid(int uid) {
        return count(new QueryWrapper<Post>().eq("uid", uid));
    }

    @Override
    public Post getModifyAndWait(int pid) {
        return this.getOne(new QueryWrapper<Post>().eq("id", pid).select("modify", "is_wait", "is_enable"));
    }

    @Override
    public boolean updateWait(int pid, boolean wait) {
        Post showPost = new Post();
        showPost.setIsWait(wait);
        return this.update(showPost, new UpdateWrapper<Post>().eq("id", pid));
    }

    @Override
    public boolean updateEnable(int pid, boolean enable) {
        Post showPost = new Post();
        showPost.setIsEnable(enable);
        return this.update(showPost, new UpdateWrapper<Post>().eq("id", pid));
    }
}
